Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testutil: support empty string args in fakecommand #244

Merged
merged 1 commit into from
Aug 1, 2023

Conversation

flotter
Copy link
Contributor

@flotter flotter commented Jun 22, 2023

testutil: support empty string args in fakecommand

FakeCommand allows unit tests to check which command-line arguments
were passed, by recording a log of calls with their arguments.

The following examples cannot be tested with FakeCommand:

  1. shutdown -r "+0" ""

  2. echo ""

  3. configure --program-prefix ""

The empty string argument confuses the command parser in testutil.

The issue is that both the argument (\000) and command delimiter
(\000\000) uses null characters, which makes the command split
operation split on the first occurrence of two null chars. This
is an invalid assumption for an empty string argument, which will
be presented as two argument delimiters, without anything in
between, appearing like a command delimiter.

To add support for this:

  • Change the command delimiter from \000\000 => \000\f\n\r (magic sequence)

  • Update the Calls() function to split the lines correctly.

Testutil can now be used for testing the following command, including the
case where msg is an empty string for the wall message:

shutdown [OPTIONS...] [TIME] [WALL...]

cmd := exec.Command("shutdown", "-r", fmt.Sprintf("+%d", mins), msg)

@flotter flotter changed the title testutil: support empty string cmd args testutil: support empty string cmd args in fakecommand Jun 22, 2023
@flotter flotter changed the title testutil: support empty string cmd args in fakecommand testutil: support empty string args in fakecommand Jun 22, 2023
FakeCommand allows unit tests to check which command-line arguments
were passed, by recording a log of calls with their arguments.

The following examples cannot be tested with FakeCommand:

1. shutdown -r "+0" ""

2. echo ""

3. configure --program-prefix ""

The empty string argument confuses the command parser in testutil.

The issue is that both the argument (\000) and command delimiter
(\000\000) uses null characters, which makes the command split
operation split on the first occurrence of two null chars. This
is an invalid assumption for an empty string argument, which will
be presented as two argument delimiters, without anything in
between, appearing like a command delimiter.

To add support for this:

- Change the command delimiter from \000\000 => \000\f\n\r (magic sequence)

- Update the Calls() function to split the lines correctly.

Testutil can now be used for testing the following command, including the
case where msg is an empty string for the wall message:

shutdown [OPTIONS...] [TIME] [WALL...]

cmd := exec.Command("shutdown", "-r", fmt.Sprintf("+%d", mins), msg)
@flotter
Copy link
Contributor Author

flotter commented Jun 22, 2023

This was an enhancement I did for #243 which is now closed.

Copy link
Contributor

@benhoyt benhoyt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine with me.

@flotter flotter added the Simple Nice for a quick look on a minute or two label Jun 28, 2023
@jnsgruk
Copy link
Member

jnsgruk commented Aug 1, 2023

@flotter you mentioned that this was for #243 which is now closed -- is this PR still relevant? If so I think we're good to merge it.

@flotter
Copy link
Contributor Author

flotter commented Aug 1, 2023

@jnsgruk I did the work to enhance the testutil library to test the changes in the now closed PR. When I closed the PR, I decided that this may be a useful enhancement to keep, so I broke this out into its own PR.

@jnsgruk jnsgruk merged commit f3d036c into canonical:master Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Simple Nice for a quick look on a minute or two
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants